<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :show-close="false"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="车牌号" prop="car_num">
          <el-input v-model="ruleForm.car_num" size="mini" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车型">
          <el-input v-model="ruleForm.car_type" size="mini" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="用途">
          <el-input v-model="ruleForm.car_use" size="mini" placeholder="请输入车辆用途" />
        </el-form-item>
        <el-form-item label="是否配司机">
          <el-radio v-model="ruleForm.car_config" size="mini" :label="1">是</el-radio>
          <el-radio v-model="ruleForm.car_config" size="mini" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="司机">
          <!--          <el-input v-model="ruleForm.car_driver" size="mini" placeholder="请输入司机姓名" />-->
          <el-select
            v-model="ruleForm.car_driver"
            
            size="mini"
            placeholder="请选择司机"
            style="width: 100%"
          >
            <el-option
              v-for="item in drivers"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" size="mini" @click="resetForm">取消</el-button>
          <el-button type="primary" size="mini" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { detailCar, updateCar, createCar, workerAllList } from '@/api/operations'
import { getToken } from '@/utils/auth'
export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      drivers: [], // 司机列表
      header: {},
      baseURL: process.env.VUE_APP_BASE_API,
      ruleForm: {
        car_driver: '',
        car_num: '',
        car_type: '',
        car_use: '',
        car_config: 1
      },
      rules: {
        group_name: [
          { required: true, message: '请输入组名称', trigger: 'blur' }
        ],
        telephone: [
          { validator: function(rule, value, callback) {
            if (/^1[345789]\d{9}$/.test(value) === false) {
              callback(new Error('请输入正确的联系方式'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.header['Authorization'] = 'Bearer ' + getToken()
    this.header['Lang'] = 'zh-CN'
    this.getDriver()
    // 编辑
    if (this.id) {
      this.getDetailById(this.id)
    }
  },
  methods: {
    // 司机列表
    getDriver() {
      workerAllList({
        sort: 'created_at',
        offset: 1,
        limit: 1,
        order: 'ascending',
        group_name: '',
        role_ids: [7]
      }).then(res => {
        this.drivers = res.data.admins

      })
    },
    // 获取详情
    getDetailById(id) {
      detailCar({ id }).then(res => {
        this.ruleForm = res.data
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.id) {
            this.ruleForm.car_driver=this.ruleForm.car_driver.toString()
            //  是编辑
            updateCar(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createCar(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file)
      this.ruleForm.company_logo = res.data.realPath
    },
    handleAvatarSuccess1(res, file) {
      this.ruleForm.company_cert = res.data.realPath
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 时间选择
    timeConfirm(e) {
      this.ruleForm.company_start = e[0]
      this.ruleForm.company_end = e[1]
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 10px;
    border: 1px dashed #cccccc;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 10px;
  }
</style>
